//
//  ViewController.m
//  UI11_TableView编辑 cell删除, 添加, 移动
//
//  Created by LGC on 16/3/23.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TabView.h"
#import "TableViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)TabView *tableView;

@property (nonatomic, retain)NSMutableArray *arrOfData;

@end

@implementation ViewController

- (void)dealloc {
    [_tableView release];
    [_arrOfData release];
    [super dealloc];
}

#pragma mark - Data 
- (void)handleData {
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"DLS160101-2" ofType:@"plist"];
    self.arrOfData = @[@"zhangsan", @"lisi", @"wangwu"].mutableCopy;
    
}

#pragma mark - TableView 
- (void)createTableView {
    self.tableView = [[TabView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
}

#pragma mark - tableView编辑.(插入, 删除)
/**
 * 思路:
 *     1. 让TabView进入编辑状态.
 *     2. 允许哪些行可以编辑.(默认是所有行)
 *      UITableViewDataSource
 *     3. 设置每一行的编辑样式.(删除还是插入)
 *      UITableViewDelegate
 *     4. 提交编辑.
 *      UITableViewDataSource
 */


#pragma mark - dataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrOfData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *poo1 = @"pool";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:poo1];
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:poo1]autorelease];
        
    }
    cell.textLabel.text = [self.arrOfData objectAtIndex:indexPath.row];
    
    
    return cell;
}



- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    
    // 进入/退出编辑状态.
    [self.tableView setEditing:editing animated:animated];
    
}

/** 哪些行可以编辑. */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 除了第一行, 哪些行可以编辑.
    if (indexPath.row == 0) {
        return NO;
    }
    else {
        return YES;
    }
}

- (void)tableView:(UITableView * _Nonnull)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 删除相应的row
        [self.arrOfData removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        [self.arrOfData insertObject:@"lili" atIndex:indexPath.row];
        
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - 知识点2: tableView移动.
/**
 * 思路：
 * 1.tableView进入编辑状态
 * 2.哪些行可以移动(API:DataSource)
 * 3.移动（API：DataSource）
 */
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

- (void)tableView:(UITableView * _Nonnull)tableView moveRowAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
    
    // 更新数据.
    NSString *source = [self.arrOfData objectAtIndex:sourceIndexPath.row];
    
    [source retain];
    
    [self.arrOfData removeObjectAtIndex:sourceIndexPath.row];
    [self.arrOfData insertObject:source atIndex:destinationIndexPath.row];
    
    [source release];
    
}

#pragma mark - UITableViewDelegate
/** 每行编辑风格 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        return UITableViewCellEditingStyleInsert;
    }
    else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"tableView编辑";
    [self handleData];
    [self createTableView];
    
    // 系统提供的一个editItem, 并且已经封装好的action(setEditing:animated)
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(textNext:)];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 跳转下一页.
- (void)textNext:(UIBarButtonItem *)buttonItem {
    TableViewController *tab = [[TableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController: tab animated: YES];
    [tab release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
    
    
    
}

@end
